package com.sanpu.kxe.business.card.view;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.card.bean.BankInfoOutput;
import com.sanpu.kxe.business.card.contract.BankCardDetailsContract;
import com.sanpu.kxe.business.card.contract.CardConfig;
import com.sanpu.kxe.business.card.presenter.BankCardDetailsPresenter;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.common.interfaces.UrlConstants;
import com.sanpu.kxe.common.utils.CommonUtils;
import com.sanpu.kxe.jump.imageloader.JKImageLoader;
import com.sanpu.kxe.module.roundview.RoundTextView;
import com.sanpu.kxe.utils.VerifyUtil;

import butterknife.Bind;

/**
 * Description：银行卡详情
 * Created by 薛勇军 on 2018/7/17.
 */

public class BankCardDetailsActivity extends BaseTitleActivity implements BankCardDetailsContract.View {

    @Bind(R.id.card_image)
    ImageView vCardImage;
    @Bind(R.id.bank_name)
    TextView vBankName;
    @Bind(R.id.bankNumberTv)
    TextView bankNumberTv;
    @Bind(R.id.nameTv)
    TextView vName;
    @Bind(R.id.tvTelePhone)
    TextView tvTelePhone;
    @Bind(R.id.refundDayLl)
    LinearLayout refundDayLl;
    @Bind(R.id.billDayTv)
    TextView billDayTv;
    @Bind(R.id.refundDayTv)
    TextView refundDayTv;
    @Bind(R.id.updateRtv)
    RoundTextView updateRtv;

    private BankCardDetailsContract.Presenter mPresenter;

    private BankInfoOutput mInfoOutput;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_bankcard_details;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("银行卡详情");
        commonTitle.showRightTextView("删除");
    }

    @Override
    protected void initData() {
        super.initData();
        mInfoOutput = (BankInfoOutput)
                getIntent().getSerializableExtra(CardConfig.INTENT_BANKINFO_OUTPUT);
        if (mInfoOutput != null) {
            JKImageLoader.getInstance().displayImage(mContext,
                    UrlConstants.getBaseUrl() + mInfoOutput.getLogoPath(), vCardImage);
            if (!TextUtils.isEmpty(mInfoOutput.getBankName())) {
                vBankName.setText(mInfoOutput.getBankName());
            }
            if (!TextUtils.isEmpty(mInfoOutput.getBankNo())) {
                bankNumberTv.setText(VerifyUtil.secretBankCardNumber(mInfoOutput.getBankNo()));
            }
            if (!TextUtils.isEmpty(mInfoOutput.getAccountName())) {
                vName.setText(CommonUtils.secretIdName(mInfoOutput.getAccountName()));
            }
            if (!TextUtils.isEmpty(mInfoOutput.getRsdTel())) {
                tvTelePhone.setText(mInfoOutput.getRsdTel());
            }
            if (mInfoOutput.isDefStlFlg()) {
                commonTitle.showRightTextView("修改");
            } else {
                refundDayLl.setVisibility(View.VISIBLE);
                updateRtv.setText("修改卡信息");
                if (!TextUtils.isEmpty(mInfoOutput.getBillDay())) {
                    billDayTv.setText("每月" + mInfoOutput.getBillDay() + "号");
                }
                if (!TextUtils.isEmpty(mInfoOutput.getDueDay())) {
                    refundDayTv.setText("每月" + mInfoOutput.getDueDay() + "号");
                }
            }
        }
        String chkSts = getIntent().getStringExtra(CardConfig.INTENT_CHKSTS);
        mPresenter = new BankCardDetailsPresenter(this, mInfoOutput, chkSts);
    }

    @Override
    protected void setListener() {
        super.setListener();
        commonTitle.setRightOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String rightText = commonTitle.getRightTextView().getText().toString();
                if (TextUtils.equals("修改", rightText)) {
                    mPresenter.updateDefBank();
                } else {
                    mPresenter.deleteBank();
                }
            }
        });
        updateRtv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.openUpdateBankTelActivity();
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CommonConfig.REQUESTCODE_UPDATE_BANKTEL && resultCode == RESULT_OK && data != null) {
            String rsdTel = data.getStringExtra(CardConfig.INTENT_RSDTEL);
            if (!TextUtils.isEmpty(rsdTel)) {
                tvTelePhone.setText(rsdTel);
                mPresenter.updateRsdTel(rsdTel);
            }
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(BankCardDetailsContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
